package com.jhgk.jhcee.common.resultful;

import com.jhgk.jhcee.common.enums.BasisResultEnum;
import com.jhgk.jhcee.common.enums.BasisResultTemplate;
import lombok.Data;
import org.springframework.stereotype.Component;

/**
 * @author: xiezhongping
 * @description:
 * @date: 2021/8/6 10:08.
 */
@Data
public class BasisResultful<T> {
    private long code;
    private String message;
    private String type;
    private T result;

    private BasisResultful() {

    }

    BasisResultful(long code, String message, String type) {
        this.code = code;
        this.message = message;
        this.type = type;
        this.result = null;
    }

    BasisResultful(long code, String message, String type, T data) {
        this.code = code;
        this.message = message;
        this.type = type;
        this.result = data;
    }

    BasisResultful(BasisResultTemplate basisResultTemplate, T data) {
        this.code = basisResultTemplate.getCode();
        this.message = basisResultTemplate.getMessage();
        this.type = basisResultTemplate.getType();
        this.result = data;
    }

    BasisResultful(BasisResultTemplate basisResultTemplate) {
        this.code = basisResultTemplate.getCode();
        this.message = basisResultTemplate.getMessage();
        this.result = null;
    }

    public static <T> BasisResultful<T> success(T data) {
        return new BasisResultful<>(BasisResultEnum.SUCCESSFUL, data);
    }

    public static <T> BasisResultful<T> success() {
        return new BasisResultful<>(BasisResultEnum.SUCCESS);
    }

    public static <T> BasisResultful<T> fail() {
        return new BasisResultful<>(BasisResultEnum.FAIL);
    }

    public static <T> BasisResultful<T> fail(BasisResultTemplate template) {
        return new BasisResultful<>(template);
    }

    public static <T> BasisResultful<T> fail(T data) {
        return new BasisResultful<>(BasisResultEnum.FAIL, data);
    }

    public static <T> BasisResultful<T> unauthorized(T data) {
        return new BasisResultful<>(BasisResultEnum.UNAUTHORIZED,data);
    }

    public static <T> BasisResultful<T> forbidden(T data) {
        return new BasisResultful<>(BasisResultEnum.FORBIDDEN,data);
    }
}
